import datetime

from django.db import models

# Create your models here.

# 导入
from django.db import models
from django.contrib.auth.models import AbstractUser
# from administrative.models import Company
# 我们重写用户模型类, 继承自 AbstractUser
from Administrative_office_system.utils.BaseModel import BaseModel, TimeStampFileName
# from finance.models import QuickAuthorizationApplicationCategory


class User(AbstractUser, BaseModel):
    """自定义用户模型类"""

    # 工号 人员编码 姓名 行政部门名称 部门 人员类别 性别 出生日期 转正时间 人员属性 进入本院日期 手机 工位 离职日期 职员身份 职务 职位 职级 职等 第一学历 在职学历
    job_number = models.CharField(default='', max_length=64, verbose_name='工号')
    personnel_code = models.CharField(default='', max_length=64, verbose_name='人员编码')
    real_name = models.CharField(default='', max_length=64, verbose_name='姓名')
    department = models.CharField(default='', max_length=32, verbose_name='行政部门名称')
    department_code = models.CharField(default='', max_length=32, verbose_name='行政部门代码')
    personnel_category = models.CharField(default='', max_length=32, verbose_name='人员类别')
    gender = models.CharField(max_length=10, choices=(('male', '男'), ('female', '女')), default='male', verbose_name='性别')
    birthday_date = models.CharField(default='', max_length=32, verbose_name='出生日期')
    regular_time = models.CharField(default='', max_length=32, verbose_name='转正时间')
    personnel_attributes = models.CharField(default='', max_length=32, verbose_name='人员属性')
    admission_in_time = models.CharField(default='', max_length=32, verbose_name='进入本院日期')
    mobile = models.CharField(max_length=11, verbose_name='手机号')
    station = models.CharField(default='', max_length=32, verbose_name='工位')
    resignation_time = models.CharField(default='', max_length=32, verbose_name='离职日期')
    employee_identity = models.CharField(default='', max_length=32, verbose_name='职员身份')
    post = models.CharField(default='', max_length=64, verbose_name='职务')
    position = models.CharField(default='', max_length=64, verbose_name='职位')
    rank = models.CharField(default='', max_length=64, verbose_name='职级')
    positions_grade = models.CharField(default='', max_length=32, verbose_name='职等')
    first_degree = models.CharField(default='', max_length=32, verbose_name='第一学历')
    jon_degree = models.CharField(default='', max_length=32, verbose_name='在职学历')

    # mobile = models.CharField(max_length=11, verbose_name='手机号')
    # real_name = models.CharField(max_length=32, verbose_name='姓名')
    autograph = models.CharField(max_length=32, default='', verbose_name='签名')
    # job_number = models.CharField(max_length=32, verbose_name='工号')
    nation = models.CharField(max_length=32, default='', verbose_name='民族')
    ID_number = models.CharField(max_length=32, default='', verbose_name='身份证号')
    native_place = models.CharField(max_length=32, default='', verbose_name='籍贯')
    major = models.CharField(max_length=32, default='', verbose_name='专业')
    graduated_university = models.CharField(max_length=32, default='', verbose_name='毕业院校')
    is_synchronize_personnel_file = models.BooleanField(default=False, verbose_name='是否同步人事档案')
    # gender = models.CharField(max_length=10, choices=(('male', '男'), ('female', '女')), default='male',verbose_name='性别')
    is_mobile_access = models.BooleanField(default=False, verbose_name='是否允许手机访问')
    user_status = models.CharField(max_length=32, verbose_name='用户状态')
    # department = models.CharField(max_length=32,  verbose_name='部门')
    # position = models.CharField(max_length=32, verbose_name='职称')
    schedule_type = models.CharField(max_length=32, verbose_name='排班类型')
    role = models.CharField(max_length=32, verbose_name='角色')
    superior = models.CharField(max_length=32, verbose_name='上级')
    subordinate = models.CharField(max_length=32, verbose_name='下级')
    management_scope = models.CharField(max_length=32, verbose_name='管理范围')
    serial_number = models.IntegerField(default=0, verbose_name='序号')
    region = models.CharField(max_length=32, verbose_name='区域')
    country = models.CharField(max_length=32, default='中国', verbose_name='国家')
    province = models.CharField(max_length=32, default='', verbose_name='省份')
    city = models.CharField(max_length=32, verbose_name='城市')
    workplace = models.CharField(max_length=32, verbose_name='职场')
    job_category = models.CharField(max_length=32, verbose_name='岗位类别')
    marital_status = models.CharField(max_length=10, choices=(('single', '单身'), ('married', '已婚')), default='single', verbose_name='婚姻状态')
    education = models.CharField(max_length=32, verbose_name='学历')
    political_outlook = models.CharField(max_length=32, default='', verbose_name='政治面貌')
    birthday = models.CharField(default='', max_length=32, verbose_name='生日')
    age = models.IntegerField(default=0, verbose_name='年龄')
    mailbox = models.CharField(max_length=32, verbose_name='邮箱')
    QQ = models.CharField(max_length=32, verbose_name='QQ')
    wechat_number = models.CharField(max_length=32, verbose_name='微信号')
    office_telephone = models.CharField(max_length=32, default='', verbose_name='办公电话')
    home_address = models.CharField(max_length=32, verbose_name='家庭地址')
    signature_picture = models.FileField(default="", upload_to=TimeStampFileName(r'signature_picture/'), )
    enclosure = models.FileField(upload_to=TimeStampFileName(r'enclosure/'), default='')
    remarks = models.TextField(verbose_name='备注')
    is_delete = models.IntegerField(default=0, verbose_name='是否删除')
    level = models.CharField(max_length=10, verbose_name='职级：党委委员（1）、中层干部（2）、普通职员（3）', default="3")
    head_portrait = models.FileField(default="", upload_to=TimeStampFileName(r'HeadPortrait/'), )
    openid = models.CharField(max_length=100, verbose_name='微信唯一标识', default="")
    group = models.ForeignKey(
        'Group',
        verbose_name="权限分组",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    company = models.ForeignKey(
        'administrative.Company',
        verbose_name="部门",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    function_ids = models.ManyToManyField('Function', verbose_name='职能分组',  related_name='function_ids_users', blank=True)

    # 对当前表进行相关设置:
    class Meta:
        db_table = 'tb_users_message'
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    # 在 str 魔法方法中, 返回用户名称
    def __str__(self):
        return '%s(%s)' % (self.real_name, self.username)


class Function(BaseModel):
    packet = models.CharField('分组', max_length=10)
    describe = models.CharField('描述', max_length=50, blank=True)

    class Meta:
        verbose_name = '用户职能分组'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.packet


class Scheduling(BaseModel):
    DAY = (('a', '白班'),
           ('b', '夜班'),)
    TYPE = (('a', '一线班'),
            ('b', '二线班'),)
    type = models.CharField(max_length=11, verbose_name='排班类型', choices=TYPE, default='b')
    user = models.ForeignKey(
        'User',
        verbose_name="人员",
        blank=False,
        null=False,
        on_delete=models.CASCADE)

    day_type = models.CharField('班次类型', max_length=10, choices=DAY, default='b')
    is_work = models.BooleanField(default=False, verbose_name='是否排班')
    last_date = models.DateField(verbose_name='上次值班日期', default=datetime.date(1900,1,1))
    date = models.DateField(verbose_name='值班日期')

class SchedulingCopy(BaseModel):
    DAY = (('a', '白班'),
           ('b', '夜班'),)
    TYPE = (('a', '一线班'),
            ('b', '二线班'),)
    type = models.CharField(max_length=11, verbose_name='排班类型', choices=TYPE, default='b')
    user = models.ForeignKey(
        'User',
        verbose_name="人员",
        blank=False,
        null=False,
        on_delete=models.CASCADE)

    day_type = models.CharField('班次类型', max_length=10, choices=DAY, default='b')
    is_work = models.BooleanField(default=False, verbose_name='是否排班')
    last_date = models.DateField(verbose_name='上次值班日期', default=datetime.date(1900,1,1))
    date = models.DateField(verbose_name='值班日期')

class SortLine(models.Model):
    DAY = (('a', '白班'),
           ('b', '夜班'),)
    user = models.ForeignKey(
        'User',
        verbose_name="人员",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    type = models.CharField(max_length=11, verbose_name='排班类型')
    date = models.DateField(verbose_name='值班日期')
    day_type = models.CharField('班次类型', max_length=10, choices=DAY, default='b')
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除')


class Education(BaseModel):
    """
    用户地址
    """
    # user = models.ForeignKey(User, related_name='addresses',verbose_name='用户')
    user_id = models.IntegerField(verbose_name='用户表id')
    start_time = models.CharField(max_length=20, verbose_name='入学时间')
    end_time = models.CharField(max_length=20, verbose_name='毕业时间')
    education = models.CharField(max_length=32, verbose_name='学历')
    school = models.CharField(max_length=128, verbose_name='学校')
    current_status = models.CharField(max_length=32, verbose_name='目前状态')
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_education'
        verbose_name = '用户教育经历'
        verbose_name_plural = verbose_name
        ordering = ['-update_time']


class Schedule(BaseModel):
    user_id = models.IntegerField(verbose_name='用户表id')
    title = models.CharField(max_length=255, verbose_name='标题')
    end_time = models.DateTimeField(verbose_name='时间')
    remarks = models.CharField(max_length=32, verbose_name='备注')
    station_prompt = models.BooleanField(default=False, verbose_name='站内提示')
    sms_prompt = models.BooleanField(default=False, verbose_name='短信提示')
    email_prompt = models.BooleanField(default=False, verbose_name='邮件提示')
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_schedule'
        verbose_name = '用户待办事项'
        verbose_name_plural = verbose_name
        ordering = ['-update_time']


class WorkExperience(BaseModel):
    user_id = models.IntegerField(verbose_name='用户表id')
    start_time = models.CharField(max_length=20, verbose_name='入职时间')
    end_time = models.CharField(max_length=20, verbose_name='离职时间')
    department = models.CharField(max_length=32, verbose_name='科室')
    position = models.CharField(max_length=128, verbose_name='职位')
    hospital = models.CharField(max_length=32, verbose_name='医院')
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_user_work_experience'
        verbose_name = '用户工作经历'
        verbose_name_plural = verbose_name
        ordering = ['-update_time']


class Group(BaseModel):
    level = models.CharField('级别', max_length=10)
    describe = models.CharField('描述', max_length=50, blank=True)

    class Meta:
        verbose_name = '用户群组'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.level


class BillGroup(BaseModel):
    TYPE = (
        ('pc', 'pc端'),
        ('app', '移动端'),
    )
    menu = models.CharField('菜单', max_length=50)
    bill = models.CharField('单据', max_length=50)
    apply_user = models.TextField(verbose_name='申请人工号')
    apply_user_name = models.TextField(verbose_name='申请人名字')
    groups = models.ManyToManyField('Group', verbose_name='权限', blank=True)
    executor = models.ManyToManyField('User', verbose_name='可执行人', blank=True)
    type = models.CharField('类型', max_length=10, choices=TYPE, default='pc')


class ConferenceRoomApplication(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
        ('revoke', '撤销')
    )
    names = models.CharField('申请人', max_length=50)
    department = models.CharField('申请部门', max_length=50)
    particulars = models.CharField('事由', max_length=255)
    statr_time = models.DateTimeField('开始时间')
    end_time = models.DateTimeField('结束时间')
    hour_statr_time = models.DateTimeField('整点开始时间', auto_now_add=False)
    hour_end_time = models.DateTimeField('整点结束时间', auto_now_add=False)
    meeting_root = models.CharField('会议室', max_length=50)
    Number_people = models.CharField('参会人数', max_length=50)
    conferee = models.TextField('参会人员')
    status = models.CharField('会议状态', max_length=50, default=0)  # 0、未开始，1、进行中，2、已结束，3、已撤回
    audit_people = models.ForeignKey('User', verbose_name="审批人", blank=True, null=True, related_name='CurrentAuditUsers', on_delete=models.CASCADE)
    apply_user = models.ForeignKey('User', verbose_name="申请人", blank=True, null=True, related_name='ApplyUsers', on_delete=models.SET_NULL)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    audit_users_already = models.ManyToManyField('User', verbose_name='已审核人员', related_name='conference_already_audit_users', blank=True)
    uuid = models.CharField(max_length=128, verbose_name='uuid', default='')
    SignInCode = models.CharField(max_length=128, verbose_name='是否生成签到二维码', default='')

    def get_state_display(self):
        if self.state == "draft":
            return '拒绝'
        if self.state == "audit":
            return '结束'
        if self.state == "unaudit":
            return '审核中'
        if self.state == "revoke":
            return '撤销'


class ConferenceRoomSignee(BaseModel):
    parent_id = models.ForeignKey('ConferenceRoomApplication', verbose_name="会议室订阅", blank=False, null=False, on_delete=models.CASCADE)
    SigneeCode = models.ForeignKey('User', verbose_name="签到人", blank=True, null=True, on_delete=models.CASCADE)
    scan_or_not = models.CharField(max_length=10, verbose_name='是否扫码', default='0')
    leave = models.CharField(max_length=10, verbose_name='是否请假', default='0')           # 请假：1，未请假：0
    uid = models.CharField(max_length=128, verbose_name='扫码设备', default='')


class SealApply(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '科室负责人审批'),
        ('two', '分管领导审批'),
        ('three', '办公室主任审批'),
        ('four', '办公室审批'),
        ('thirteen', '盖章归档'),
        ('five', '结束'),
        ('six', '拒绝'),
        ('shelve', '搁置'),
        ('seven', '书记/院长审批'),
        ('ten', '办公室盖章审批'),
        ('nine', '审计科审核'),
        ('cancel', '作废'),
        ('stop', '终止'),
        ('recall', '撤回')
    )
    MATTER_TYPE = (
        ('a', '由合同会签直转'),
        ('b', '由行政公文直转'),
    )

    number = models.CharField('编号', max_length=50)
    apply_user = models.ForeignKey(
        'User',
        verbose_name="申请人",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    is_fast = models.BooleanField('是否快捷办理', default=False)
    leval = models.CharField('授权级别', max_length=10, default='')
    authorization_type = models.CharField('授权类型', max_length=10, default='')
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    types = models.CharField('直转类型', max_length=10, choices=MATTER_TYPE, default='a')
    matter_type_text = models.TextField(verbose_name='用印事由')
    seal_name = models.TextField(verbose_name='印章名称')
    credentials = models.TextField(verbose_name='证件名称', blank=True, null=True)
    current_audit_users = models.ManyToManyField('User', verbose_name='当前审核人员', related_name='seal_apply_users', blank=True)
    audit_users_already = models.ManyToManyField('User', verbose_name='已审核人员', related_name='seal_apply_already_audit_users', blank=True)
    quick_category = models.ManyToManyField('finance.QuickAuthorizationApplicationCategory', verbose_name='快捷申请类型', related_name='quick_application_category', blank=True)
    transfer = models.CharField('是否直转（由合同会签直转）', max_length=50, blank=True, null=True, default='0')
    offline_printing = models.CharField('是否线下用印', max_length=10, blank=True, null=True, default='0')
    countersign_id = models.ForeignKey(
        'finance.BusinessContract',
        verbose_name="合同会签ID",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    conference_id = models.ForeignKey(
        'administrative.ConferenceMemo',
        verbose_name="行政公文ID",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    servings = models.IntegerField(verbose_name='用印份数')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    fast_number = models.CharField('快捷编号', max_length=32, default='', blank=True)
    voucher = models.CharField('是否上传凭证', max_length=32, default='0', blank=True)
    note = models.TextField(verbose_name='备注')
    FileType = models.CharField('文件类型', max_length=32, default='1', blank=True)
    fg_id = models.CharField(verbose_name='分管领导id', max_length=16, default='', null=True)
    shouquan = models.CharField(verbose_name="授权项目", max_length=512, default='', null=True)
    last_state = models.CharField(verbose_name='搁置之前的流程节点', max_length=16, default='', null=True)
    whether_upload = models.CharField('是否上传', max_length=10, default='0')

    def get_state_str(self):
        if self.state == 'five':
            return '结束'
        elif self.state == 'six':
            return '拒绝'
        elif self.state == 'cancel':
            return '作废'
        elif self.state == 'stop':
            return '终止'
        elif self.state == 'recall':
            return '撤回'
        elif self.state == 'draft':
            return '草稿'
        elif self.state == 'shelve':
            return '搁置'
        else:
            return '审批中'


class SealApplyAuditRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
        ('referral', '转审'),
        ('shelve', '搁置')
    )
    NODE = (
        ('C/D', '科室负责人'),
        ('B', '分管领导'),
        ('ys', '院长授权'),
        ('YB', '院办主任'),
        ('YZ', '书记/院长'),
        ('sjk', '审计科'),
        ('BGS', '办公室'),
        ('stb', '审计科转办公室'),
    )
    parent_id = models.ForeignKey(
        'SealApply',
        verbose_name="用印申请",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey(
        'User',
        verbose_name="审批人",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10, choices=NODE, default='C/D')

    class Meta:
        managed = True
        verbose_name = '用印申请审批信息'
        verbose_name_plural = verbose_name


class SmsVerificationCode(BaseModel):
    NODE = (
        ('a', '绑定手机号'),
        ('b', '修改密码')
    )
    mobile = models.CharField('手机号', max_length=50)
    code = models.CharField('验证码', max_length=30)
    start_time = models.DateTimeField(verbose_name='生效时间')
    end_time = models.DateTimeField(verbose_name='失效时间')
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除')
    type = models.CharField('类型', max_length=10, choices=NODE, default='a')

    class Meta:
        managed = True
        verbose_name = '短信验证码'
        verbose_name_plural = verbose_name


class Product(BaseModel):
    number = models.CharField('编号', max_length=128)
    name = models.CharField('物资名称', max_length=50)
    unit = models.CharField('单位', max_length=10)

    def get_real_qty(self):
        try:
            obj = RealTimeInventory.objects.get(product_id=self.id)
        except Exception as e:
            return 0
        else:
            return obj.qty


class AssetRegistration(BaseModel):
    apply_user = models.ForeignKey(
        'User',
        verbose_name="创建人",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    company = models.ForeignKey(
        'administrative.Company',
        verbose_name="部门",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    number = models.CharField('编号', max_length=128)
    product = models.ForeignKey('Product', verbose_name="物资", blank=True,
                                null=True,
                                on_delete=models.SET_NULL)
    type = models.CharField('资产类型', max_length=50)
    quantity = models.IntegerField(verbose_name="数量")
    # unit = models.CharField('单位', max_length=10)
    purchase_date = models.DateField(verbose_name='领用日期')
    note = models.TextField(verbose_name="备注")
    uuid = models.CharField(max_length=128, verbose_name='uuid', default='')


class RealTimeInventory(BaseModel):
    product = models.ForeignKey('Product', verbose_name="物资", blank=True,
                                null=True,
                                on_delete=models.SET_NULL)
    qty = models.IntegerField(verbose_name="剩余数量")


class ProductAdopt(BaseModel):
    STATE = (
        ('one', '审核中'),
        ('two', '结束'),
        ('cancel', '作废'),
        ('four', '拒绝'),
        ('recall', '撤回'),
        ('stop', '终止'),
    )
    number = models.CharField(verbose_name='编号', max_length=100)
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    apply_user = models.ForeignKey(
        'User',
        verbose_name="创建人",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    company = models.ForeignKey(
        'administrative.Company',
        verbose_name="部门",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    # total_amount = models.FloatField(verbose_name='总金额')
    total_qty = models.IntegerField(verbose_name="总数量")
    date = models.DateField(verbose_name='领用日期')
    current_audit_users = models.ManyToManyField('User', verbose_name='当前审核人员',
                                                 related_name='product_adopt_users', blank=True)
    audit_users_already = models.ManyToManyField('User', verbose_name='已审核人员',
                                                 related_name='product_adopt_already_audit_users', blank=True)
    note = models.TextField(verbose_name="备注", blank=False)

    def get_str_state(self):
        if self.state == 'one':
            return '审核中'
        elif self.state == 'two':
            return '结束'
        elif self.state == 'cancel':
            return '作废'
        elif self.state == 'four':
            return '拒绝'
        elif self.state == 'recall':
            return '撤回'
        elif self.state == 'stop':
            return '终止'



class ProductAdoptLine(BaseModel):
    adopt = models.ForeignKey('ProductAdopt', verbose_name="物料领用单", blank=True,
                                 null=True,
                                 on_delete=models.SET_NULL)
    product = models.ForeignKey('Product', verbose_name="物资", blank=True,
                                null=True,
                                on_delete=models.SET_NULL)
    qty = models.IntegerField(verbose_name="数量")
    unit = models.CharField(verbose_name='单位', max_length=10)
    # price = models.FloatField(verbose_name='单价')
    # amount = models.FloatField(verbose_name='金额')


class ProductAdoptAuditRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    parent_id = models.ForeignKey(
        'ProductAdopt',
        verbose_name="物料领用单",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey(
        'personal_center.User',
        verbose_name="审批人",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10)
    seq = models.IntegerField(default=0, verbose_name='顺序')


class ProductPurchase(BaseModel):
    STATE = (
        ('one', '审核中'),
        ('two', '结束'),
        ('cancel', '作废'),
        ('four', '拒绝'),
        ('recall', '撤回'),
        ('stop', '终止'),
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    number = models.CharField(verbose_name='编号', max_length=100)
    apply_user = models.ForeignKey(
        'User',
        verbose_name="创建人",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    company = models.ForeignKey(
        'administrative.Company',
        verbose_name="部门",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    total_amount = models.FloatField(verbose_name='总金额')
    date = models.DateField(verbose_name='采购日期')
    current_audit_users = models.ManyToManyField('User', verbose_name='当前审核人员',
                                                 related_name='product_purchase_users', blank=True)
    audit_users_already = models.ManyToManyField('User', verbose_name='已审核人员',
                                                 related_name='product_purchase_already_audit_users', blank=True)

    def get_state_display(self):
        if self.state == 'one':
            return '审核中'
        elif self.state == 'two':
            return '结束'
        elif self.state == 'cancel':
            return '作废'
        elif self.state == 'four':
            return '拒绝'
        elif self.state == 'recall':
            return '撤回'
        elif self.state == 'stop':
            return '终止'


class ProductPurchaseLine(BaseModel):
    adopt = models.ForeignKey('ProductPurchase', verbose_name="物料采购单", blank=True,
                                 null=True,
                                 on_delete=models.SET_NULL)
    product = models.ForeignKey('Product', verbose_name="物资", blank=True,
                                null=True,
                                on_delete=models.SET_NULL)
    qty = models.IntegerField(verbose_name="数量")
    real_qty = models.IntegerField(verbose_name="实收数量", default=0)
    unit = models.CharField(verbose_name='单位', max_length=10)
    price = models.FloatField(verbose_name='单价')
    amount = models.FloatField(verbose_name='金额')


class ProductPurchaseAuditRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    parent_id = models.ForeignKey(
        'ProductPurchase',
        verbose_name="物料采购单",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey(
        'personal_center.User',
        verbose_name="审批人",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10)
    seq = models.IntegerField(default=0, verbose_name='顺序')


class StockManage(BaseModel):
    TYPE = (
        ('inner', '入库'),
        ('out', '出库')
    )
    STATE = (
        ('draft', '新建'),
        ('confirm', '确认')
    )
    number = models.CharField(verbose_name='编号', max_length=100)
    state = models.CharField('状态', max_length=10, choices=STATE, default='draft')
    adopt = models.ForeignKey('ProductAdopt', verbose_name="物料领用单", blank=True,
                              null=True,
                              on_delete=models.SET_NULL)
    purchase = models.ForeignKey('ProductPurchase', verbose_name="物料采购单", blank=True,
                                 null=True,
                                 on_delete=models.SET_NULL)
    type = models.CharField('类型', max_length=10, choices=TYPE, default='inner')
    current_audit_users = models.ManyToManyField('User', verbose_name='当前审核人员',
                                                 related_name='stock_manage_users', blank=True)
    audit_users_already = models.ManyToManyField('User', verbose_name='已审核人员',
                                                 related_name='stock_manage_already_audit_users', blank=True)


class StockManageLine(BaseModel):
    stock = models.ForeignKey('StockManage', verbose_name="出入库单", blank=True,
                              null=True,
                              on_delete=models.SET_NULL)
    product = models.ForeignKey('Product', verbose_name="物资", blank=True,
                                null=True,
                                on_delete=models.SET_NULL)
    qty = models.IntegerField(verbose_name="数量")
    real_qty = models.IntegerField(verbose_name="实收数量", default=0)
    unit = models.CharField(verbose_name='单位', max_length=10)
    price = models.FloatField(verbose_name='单价', default=0)
    amount = models.FloatField(verbose_name='金额', default=0)


class AuditForm(BaseModel):
    apply_user = models.ForeignKey(
        'User',
        verbose_name="创建人",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    name = models.CharField(max_length=255, verbose_name='名称')
    process_html = models.TextField(verbose_name="流程表单html")
    process_json = models.TextField(verbose_name="流程")


# 印章图片
class seal(BaseModel):
    pic = models.ImageField(upload_to='pic/', verbose_name=u'图片地址')
    watermark_pic = models.CharField(max_length=225, verbose_name="水印图片地址", default="")
    pic_name = models.CharField(max_length=100, verbose_name="图片名称")
    seal_name = models.CharField(max_length=225, verbose_name="印章名称", default="")
    save_prople = models.CharField(max_length=100, verbose_name="保管人")
    explain = models.TextField(verbose_name="印章说明")


class FixedAssetsAuditRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('B', '分管领导'),
        ('SJ/YZ', '书记/院长'),
        ('SBKZ', '设备科长'),
        ('CWKZ', '财务科长'),
        ('CWK', '财务科')
    )
    parent_id = models.ForeignKey(
        'FixedAssets',
        verbose_name="固定资产付款",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey(
        'User',
        verbose_name="审批人",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10, choices=NODE, default='SBKZ')

    # class Meta:
    #     managed = True
    #     verbose_name = '用印申请审批信息'
    #     verbose_name_plural = verbose_name


class FixedAssets(BaseModel):
    STATE = (
        ('one', '设备科长审核'),
        ('two', '财务科长审核'),
        ('three', '设备科分管院长签字'),
        ('four', '书记/院长审批'),
        ('five', '财务科支付'),
        ('six', '结束'),
        ('seven', '拒绝'),
        ('cancel', '作废'),
        ('stop', '终止'),
        ('recall', '撤回'),
        ('draft', '草稿')
    )
    number = models.CharField('编号', max_length=50)
    apply_user = models.ForeignKey(
        'User',
        verbose_name="申请人",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    title = models.CharField(max_length=100, verbose_name="付款标题")
    total_money = models.CharField(max_length=255, verbose_name='付款总金额', default="")
    company = models.ForeignKey(
        'administrative.Company',
        verbose_name="上传科室",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    financial = models.CharField(max_length=225, verbose_name="经费来源", default="")
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    current_audit_users = models.ManyToManyField('User', verbose_name='当前审核人员', related_name='fixed_assets_users', blank=True)
    audit_users_already = models.ManyToManyField('User', verbose_name='已审核人员', related_name='fixed_assets_already_audit_users', blank=True)
    detail_pay = models.TextField(verbose_name='付款明细', default='')
    daxie = models.CharField(max_length=255, verbose_name='付款总金额大写', default='')
    remake = models.CharField(max_length=500, verbose_name='备注', default='')
    can_revise = models.CharField('是否可以修改', max_length=50, default='1')
    payee = models.CharField('收款单位', max_length=255, default='')
    payee_account = models.CharField('收款账号', max_length=32, default='')
    def get_str_state(self):
        if self.state == 'six':
            return '结束'
        elif self.state == 'seven':
            return '拒绝'
        elif self.state == 'cancel':
            return '作废'
        elif self.state == 'stop':
            return '终止'
        elif self.state == 'recall':
            return '撤回'
        elif self.state == 'draft':
            return '草稿'
        else:
            return '审核中'

# 用印编号
class SealApplyNumber(BaseModel):
    current_number = models.CharField(max_length=20, verbose_name="当前编号", default="")
    next_number = models.CharField(max_length=20, verbose_name="下一编号", default="")
    time_node = models.CharField(max_length=50, verbose_name="时间节点", default="")

# 抄送
class MakeCopy(BaseModel):
    TYPE = (
        ('a', '用印申请'),
        ('b', '行政公文'),
        ('c', '快捷用印授权'),
        ('d', '服务申请'),
        ('e', '物资服务申请'),
        ('f', '合同会签'),
        ('g', '维修改造'),
        ('h', '计算机及相关设备购置'),
        ('i', '项目审计送审申请'),
        ('j', '招标采购项目'),
        ('l', '议题抄送'),
        ('m', '差旅申请'),
        ('n', '中层干部外出'),
        ('o', '数据外导'),
        ('p', '新合同会签'),
        ('q', '体检科支援加班补助'),
        ('r', '离职申请'),
    )
    recipient = models.ForeignKey(
        'User',
        verbose_name="接收人",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    type = models.CharField('类型', max_length=10, choices=TYPE, default='a')
    process_id = models.CharField('流程ID', max_length=10, default='')
    look_over = models.CharField('是否查看', max_length=10, default='0')